package com.metaera.app.ui.fragments;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.core.SimplePaginationPresenter;
import com.metaera.app.controller.view.BasicPaginationView;
import com.metaera.app.controller.view.RefreshLoadMoreView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.model.HotColumnResponse;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.adapters.HomeFollowNewsAdapter;
import com.metaera.app.ui.adapters.HotColumnAdapter;
import com.metaera.app.ui.core.BaseRequestFragment;
import com.metaera.app.utils.event.LogoutEvent;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observable;

public class FollowListFragment extends BaseRequestFragment {

    private RecyclerView recyclerView;

    private SmartRefreshLayout refreshLayout;

    private DividerItemDecoration dividerItemDecoration;

    @Override
    protected int getLayoutId() {
        return R.layout.include_smart_refresh_recycler_view;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        recyclerView = findViewById(R.id.recycler_view);
        refreshLayout = findViewById(R.id.smart_refresh_layout);

         dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
        Drawable drawable = ContextCompat.getDrawable(recyclerView.getContext(), R.drawable.shape_item_divider);
        dividerItemDecoration.setDrawable(drawable);

        if (LocalConfigStore.getInstance().isLogin()) {
            renderFollowNews();

        } else {
            renderColumnList();
        }
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    private void renderColumnList() {
        refreshLayout.setEnableLoadMore(true);
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setBackgroundColor(Color.WHITE);

        recyclerView.setAdapter(new HotColumnAdapter());

        SimplePaginationPresenter<HotColumnResponse> presenter = new SimplePaginationPresenter<HotColumnResponse>() {

            @Override
            protected Observable<BasicListResponse<HotColumnResponse>> execute() {
                return DataRepository.getInstance().getPopularColumn(ApiParam.AUTHOR_TYPE, mPage, ApiParam.PAGE_SIZE);
            }

        };

        BasicPaginationView<HotColumnResponse> paginationView =
                new BasicPaginationView<>(recyclerView, refreshLayout, this);

        paginationView.setEmptyLayoutResId(R.layout.view_state_layout_empty); // 空内容
        paginationView.setNotMoreFooterLayoutResId(R.layout.view_list_not_more); // 没有更多
        presenter.setModelAndView(paginationView);
        presenter.requestRefresh();

        // listener
        refreshLayout.setOnRefreshLoadMoreListener(new RefreshLoadMoreView<HotColumnResponse>(presenter){
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                if (LocalConfigStore.getInstance().isLogin()) {
                    renderFollowNews();

                } else {
                    super.onRefresh(refreshLayout);
                }
            }
        });
    }


    private void renderFollowNews() {
        recyclerView.removeItemDecoration(dividerItemDecoration);
        recyclerView.setBackgroundColor(getResources().getColor(R.color.colorBackground));

        recyclerView.setAdapter(new HomeFollowNewsAdapter());

        SimplePaginationPresenter<ArticleListResponse> presenter = new SimplePaginationPresenter<ArticleListResponse>() {

            @Override
            protected Observable<BasicListResponse<ArticleListResponse>> execute() {
                return DataRepository.getInstance().getFollowLists(mPage, ApiParam.PAGE_SIZE);
            }

        };

        BasicPaginationView<ArticleListResponse> paginationView =
                new BasicPaginationView<ArticleListResponse>(recyclerView, refreshLayout, this){

                    @Override
                    public void renderRefresh(List<ArticleListResponse> refreshData) {
                        if (refreshData == null || refreshData.isEmpty()){
                            renderColumnList();

                        } else {
                            super.renderRefresh(refreshData);
                        }
                    }

                };

        paginationView.setEmptyLayoutResId(R.layout.view_state_layout_empty); // 空内容
        paginationView.setNotMoreFooterLayoutResId(R.layout.view_list_not_more); // 没有更多
        presenter.setModelAndView(paginationView);
        presenter.requestRefresh();

        // listener
        refreshLayout.setOnRefreshLoadMoreListener(new RefreshLoadMoreView<>(presenter));
    }

    @Subscribe
    public void event(UserDetailsResponse user) {
        renderFollowNews();
    }

    @Subscribe
    public void event(LogoutEvent user) {
        renderColumnList();
    }

}
